<?php
class Front_Form_Utilisateurs extends Jnd_Form
{

    public function __construct( $decorateur = "Formulairejndpro", $options = null )
    {
        parent::__construct($decorateur, $options);
        $this->setElementDecorators(array(
            "$decorateur"
        ), array(), false);
    }

    public function init()
    {
        $this->setName("moncompte");
        $this->setAction("/accueil/moncompte");
        $this->setMethod("post");
        $this->addAttribs(array(
            "class" => "cssform"
        ));
        
        $champ = new Zend_Form_Element_Hidden("id");
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("nom");
        $champ->setLabel(__("Nom"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("prenom");
        $champ->setLabel(__("Prénom"));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("mail");
        $champ->setLabel(__("Mail"));
        $champs[] = $champ;
        
        $champ = new Jnd_Form_Element_DatePicker("dateNaissance");
        $champ->setLabel(__("Date de naissance"));
        $champ->setAttribs(array(
            "changeYear" => true, 
            "changeMonth" => true, 
            "yearRange" => "c-40:c+28", 
            "numberOfMonths" => 3, 
            "showButtonPanel" => true, 
            "defaultDate" => '-30y'
        ));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Textarea("adresse");
        $champ->setLabel(__("Adresse"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Textarea("adresseComplement");
        $champ->setLabel(__("Adresse complément"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("codePostal");
        $champ->setLabel(__("Code postal"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("ville");
        $champ->setLabel(__("Ville"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("telephoneFixe");
        $champ->setLabel(__("Téléphone"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("telephonePortable");
        $champ->setLabel(__("Portable"));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Text("pseudo");
        $champ->setLabel(__("Pseudo"));
        $champ->setRequired(true);
        $champ->setAttribs(array(
            "readonly" => "readonly", 
            "class" => "readonly"
        ));
        $champs[] = $champ;
        
        $champ = new Zend_Form_Element_Password("motDePasse");
        $champ->setLabel(__("Mot de passe"));
        $champ->setDescription(__("Merci d'indiquer votre mot de passe pour enregistrer les modifications."));
        $champ->setRequired(true);
        $champs[] = $champ;
        
        $this->addElements($champs);
    }

    /**
     * On contrôle que le mot de passe fournie soit bien celui du compte en
     * question
     *
     * @see Zend_Form::isValid()
     */
    public function isValid( $data )
    {
        $valid = parent::isValid($data);
        if (! $valid) {
            return false;
        }
        
        $salt = Back_Model_Utilisateurs::recuperationSalt($data['pseudo']);
        $motDePasse = $data['motDePasse'];
        $motDePasse = Jnd_Outils::generateHash($motDePasse, $salt);
        $db = Jnd_App::getResource("db");
        $adapter = new Zend_Auth_Adapter_DbTable($db, "utilisateurs", "pseudo", "mot_de_passe");
        $adapter->setIdentity($data['pseudo'])->setCredential($motDePasse);
        
        $result = $adapter->authenticate();
        if ($result->isValid()) {
            $this->removeElement("motDePasse");
            return true;
        } else {
            $valid = false;
            $this->getElement("motDePasse")->addError(__("Le mot de passe n'est pas le bon."));
            return false;
        }
    }
}

